package com.whmnx.main.ui.model;

import android.app.Application;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.whmnx.base.adapter.DynamicAdapter;
import com.whmnx.base.bean.CommentBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.main.databinding.FragmentComment2Binding;
import com.whmnx.main.ui.fragment.CommentFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;

/**
 * author : huang
 * date   : 2020/10/21 0021 19:33
 * version: 1.0
 * @author Administrator
 */
public class CommentModel2 extends BaseViewModel {

     public CommentFragment mFragment;
     public FragmentComment2Binding mBinding;
    private DynamicAdapter mDynamicAdapter;

    public CommentModel2(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(CommentFragment commentFragment, FragmentComment2Binding binding) {
        this.mFragment = commentFragment;
        this.mBinding = binding;
        mBinding.rbMainHome.setChecked(true);
        initView();
        getData(0);
    }

    private void getData(int i) {
        Map<String, Object> params = new HashMap<>();
        params.put("pId",mFragment.getArguments().getString("product_id"));
        params.put("praise",i);
        params.put("pageIndex",1);
        params.put("pageSize",20);
        HttpClient.post(mFragment.getActivity(), Constants.PRODUCTCOMMENTLIST, params,new CallBack<List<CommentBean>>() {
            @Override
            public void onSuccess(List<CommentBean> result) {
                mDynamicAdapter.replaceAll(result, true);
            }
        });
    }

    private void initView() {
        mBinding.rlList.setLayoutManager(new LinearLayoutManager(mFragment.getActivity()));
        mBinding.rlList.setItemAnimator(null);
        mDynamicAdapter = new DynamicAdapter(mFragment.getActivity());
        mBinding.rlList.setAdapter(mDynamicAdapter);
        mBinding.rlList.setFocusableInTouchMode(false);


        mBinding.rgContent.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int position = group.indexOfChild(group.findViewById(checkedId));
                switch (position) {
                    case 0:
                        getData(0);
                        break;
                    case 1:
                        getData(1);
                        break;
                    case 2:
                        getData(2);
                        break;
                    case 3:
                        getData(3);
                        break;
                    case 4:
                        getData(4);
                        break;
                    default:
                }
            }
        });
    }


}
